package mpc;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;

import Sorters.BubbleSort;
import Sorters.HeapSort;
import Sorters.QuickSort;
import Sorters.SelectionSort;
import Sorters.SlowSort;

public class MainGUI {
	static JFrame frame;
	static GridBagLayout layout;
	static Random rnd = new Random(70);
	public static Sortable[] mas = new Sortable[200];
	public static ArrayList<SortHolder> sHolders = new ArrayList<SortHolder>();
	public static boolean allFinished = false;
	public static int numberOfWinners = 0;
	
	public static void importantResets(){
		allFinished = false;
		numberOfWinners = 0;
	}
	
	public static void randomFilling(){
		Sortable[] buf = new Sortable[MainGUI.mas.length];
		for(int i = 0; i < MainGUI.mas.length; i++){
			mas[i] = new Sortable(rnd.nextInt(2000) + 1, 0);
			buf[i] = MainGUI.mas[i];
		}
		
		Arrays.sort(buf);
		for(int i = 0; i < buf.length; i++){
			buf[i].key = i;
		}
		
		importantResets();
	}
	
	public static void increasingFilling(){
		for(int i = 0; i < MainGUI.mas.length; i++){
			mas[i] = new Sortable(i, i);
			//buf[i] = MainGUI.mas[i];
		}
		importantResets();
	}
	
	public static void decreasingFilling(){
		for(int i = MainGUI.mas.length; i > 0; i--){
			mas[MainGUI.mas.length - i] = new Sortable(i, i);
			//Thread.sleep(100);
			//buf[i] = MainGUI.mas[i];
		}
		importantResets();
	}
	
	public static void initSHolsers(){
		SortHolder sh;
		GridBagConstraints c = new GridBagConstraints();
		c.ipadx = 30; c.ipady = 10;
		
		{
			sh = new SortHolder("SlowSort", mas, new SlowSort());
			c.gridx = 0; c.gridy = 1;
			
			frame.add(sh, c);
			sHolders.add(sh);
		}
		{
			sh = new SortHolder("BubbleSort", mas, new BubbleSort());
			c.gridx = 1; c.gridy = 1;
			
			frame.add(sh, c);
			sHolders.add(sh);
		}
		{
			sh = new SortHolder("HeapSort", mas, new HeapSort());
			c.gridx = 2; c.gridy = 1;
			
			frame.add(sh, c);
			sHolders.add(sh);
		}
		{
			sh = new SortHolder("SelectionSort", mas, new SelectionSort());
			c.gridx = 0; c.gridy = 2;
			
			frame.add(sh, c);
			sHolders.add(sh);
		}
		{
			sh = new SortHolder("QuickSort", mas, new QuickSort());
			c.gridx = 1; c.gridy = 2;
			
			frame.add(sh, c);
			sHolders.add(sh);
		}
		
		importantResets();
	}
	
	public static void resetSHolders(){
		for(SortHolder u : sHolders){
			u.attachMas(mas);
			//System.out.println("Массив прикреплен...");
		}
		
		importantResets();
	}
	
	public static void doAllNextIteration(){
		allFinished = true;
		
		for(SortHolder u : sHolders){
			for(int i = 0; i < 10; i++){
				u.doNextIteration();
			}
			if(!u.isFinished()) allFinished = false;
		}
	}
	
	public static void main(String[] args){
		frame = new JFrame("Сравнение сортировок");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(750, 700));
		frame.setResizable(false);
		
		layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		frame.setLayout(layout);
		
		ControlPanel cpan = new ControlPanel();
		
		c.gridx = c.gridy = 0;
		c.gridwidth = 4;
		frame.add(cpan, c);
		
		randomFilling();
		
		initSHolsers();
		{
			JLabel about = new JLabel();
			about.setFont(Font.decode("Arial-plane-10"));
			about.setText("Программу написал Антон Егоров, 11А");
			c.gridwidth = 1; c.gridx = 2; c.gridy = 3;
			c.anchor = GridBagConstraints.SOUTHEAST;
			frame.add(about, c);
		}
		 
		//for(int i = 0; i < )
		
		layout.preferredLayoutSize(frame);
		frame.pack();
		frame.setVisible(true);
	}
}
